package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Service
public class ItemServiceImpl implements ItemService {

    //  远程调用service-product-client;
    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    @Override
    public Map<String, Object> getSkuItem(Long skuId) {
        //  声明一个map 集合
        HashMap<String, Object> resultMap = new HashMap<>();

        //  获取布隆过滤器
        //        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
        //        //  判断
        //        if (!bloomFilter.contains(skuId)){
        //            return new HashMap<>();
        //        }

        //  获取到数据.skuInfo 需要一个返回值方法：
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            resultMap.put("skuInfo",skuInfo);
            return skuInfo;
        },threadPoolExecutor);

        //  获取到分类数据.
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            resultMap.put("categoryView", categoryView);
        },threadPoolExecutor);

        //  获取price.
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            resultMap.put("price", skuPrice);
        },threadPoolExecutor);

        //  获取销售属性数据.
        CompletableFuture<Void> spuSaleAttrCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            resultMap.put("spuSaleAttrList", spuSaleAttrList);
        },threadPoolExecutor);

        //  获取切换数据。
        CompletableFuture<Void> strJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map idsMap = this.productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String strJson = JSON.toJSONString(idsMap);
            resultMap.put("valuesSkuJson", strJson);
        },threadPoolExecutor);

        //  对应的海报
        CompletableFuture<Void> spuPosterCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuPoster> spuPosterList = this.productFeignClient.getSpuPosterBySpuId(skuInfo.getSpuId());
            resultMap.put("spuPosterList", spuPosterList);
        },threadPoolExecutor);

        //  获取平台属性---规格参数.
        CompletableFuture<Void> attrCompletableFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> attrList = this.productFeignClient.getAttrList(skuId);

            List<HashMap<String, Object>> attrListMap = attrList.stream().map(baseAttrInfo -> {
                HashMap<String, Object> map = new HashMap<>();
                String attrName = baseAttrInfo.getAttrName();
                //  不用循环：集合中只有一条数据！
                String valueName = baseAttrInfo.getAttrValueList().get(0).getValueName();

                map.put("attrName", attrName);
                map.put("attrValue", valueName);
                return map;
            }).collect(Collectors.toList());
            resultMap.put("skuAttrList",attrListMap);
        },threadPoolExecutor);

        //  获取热度排名：
        CompletableFuture<Void> hotCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        },threadPoolExecutor);

        //  多任务组合：需要使用all:
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                categoryViewCompletableFuture,
                priceCompletableFuture,
                spuSaleAttrCompletableFuture,
                spuPosterCompletableFuture,
                strJsonCompletableFuture,
                attrCompletableFuture,
                hotCompletableFuture).join();
        //  返回数据.
        return resultMap;
    }
}
